package com.css.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.css.entity.Constant;
import com.css.entity.Result;
import com.css.entity.User;
import com.css.util.Function;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;
import okhttp3.Call;
import okhttp3.MediaType;


/**
 * <p>
 * -
 * </p>
 *
 * @author css
 * @since 2021/4/7
 */
public class PersonService {
    public void login(String cpuId, Function<User> function) {
        OkHttpUtils.postString().url(Constant.BASE_HTTP + "/user/login")
                .addHeader("Content-Type", "application/json")
                .mediaType(MediaType.parse("application/json; charset=utf-8"))
                .content("{\"pcUuid\": \"" + cpuId + "\"}")
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int i) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onResponse(String s, int i) {
                        Result<User> result = JSON.parseObject(s, new TypeReference<Result<User>>() {
                        });
                        if (result.isSuccess()) {
                            if (function != null) {
                                function.apply(result.getData());
                            }
                        }
                    }
                });
        System.out.println(cpuId);
    }
}
